package com.java01.misc.bitwise;

public class Bitwise2 {

	public static final int MESSAGE_IS_SIGNED = 1 << 0;
	public static final int MESSAGE_IS_ENCRIPTED = 1 << 1;
	public static final int MESSAGE_HAS_ATTACHMENT = 1 << 2;
	public static final int MESSAGE_HAS_BEEN_READ = 1 << 3;
	public static final int MESSAGE_IS_SPAM = 1 << 4;

	private int messageFlag = 0;

	public boolean is(int flags) {
		return (messageFlag & flags) == flags;
	}

	public void addFlag(int flag) {
		messageFlag |= flag;
	}

	public void removeFlag(int flag) {
		messageFlag &= ~flag;
	}

	public static void main(String[] args) {

		Bitwise2 message = new Bitwise2();

		message.addFlag(MESSAGE_IS_SPAM);
		System.out.println(message.is(MESSAGE_IS_SPAM) ? "yes" : "no");
		System.out.println(message.is(MESSAGE_IS_SPAM | MESSAGE_IS_SIGNED) ? "yes" : "no");

		message.addFlag(MESSAGE_HAS_ATTACHMENT | MESSAGE_HAS_BEEN_READ);
		System.out.println(message.is(MESSAGE_HAS_ATTACHMENT | MESSAGE_HAS_BEEN_READ) ? "yes" : "no");
		System.out.println(message.is(MESSAGE_IS_SPAM | MESSAGE_HAS_ATTACHMENT) ? "yes" : "no");

		message.removeFlag(MESSAGE_IS_SPAM);
		System.out.println(message.is(MESSAGE_IS_SPAM) ? "yes" : "no");
		System.out.println(message.is(MESSAGE_HAS_ATTACHMENT | MESSAGE_HAS_BEEN_READ) ? "yes" : "no");
	}
}
